<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Application\Form\LoginForm;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;

use Application\Model\Entity\Login;

class LoginController extends AbstractActionController
{
    
    public $dbAdapter;

     public function indexAction()
    {
        return new ViewModel();
    }
    public function loginAction()
    {
        $form = new LoginForm('login');
        
        return new ViewModel(array("titulo" => "Ingresar",
            'form' => $form,
            'url' => $this->getRequest()->getBaseUrl(),
            ));
    }
    public function homeAction()
    {
        session_start();
        $this->dbAdapter=$this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        
        //$p = $this->request->getPost();
        $p = $_POST;
        $m = new Login($p,$this->dbAdapter);
        $id = $m->getIdUsuario();

//***********************************************************************************************************
        $_SESSION['id_usuario'] = $id;
//***********************************************************************************************************

        $entro = $m->autenticar();
        if ($entro){
            //$id = $m->getIdUsuario();
            $rol = $m->getRolUsuario();

//***********************************************************************************************************
	    $_SESSION['rol_usuario'] = $rol;
//***********************************************************************************************************

            $form = new LoginForm('home');
//            $form->get('id_oculto')->setAttribute('value',$id);
//            $form->get('rol_oculto')->setAttribute('value',$rol);
            return new ViewModel(array(
                'titulo' => 'Bienvenido al Sistema',
                'nombre' => $p['nombre'],
                'form' => $form,
                'rol' => $rol,
                'url'=>$this->getRequest()->getBaseUrl(),
            ));
        }else{
            //redir to login
            return $this->redirect()->toUrl($this->getRequest()->getBaseUrl().'/application/login/login');
            
        }
    }
    public function registrarseAction()
    {
        return new ViewModel(array(
            'titulo' => 'Registro de Nuevo Usuario',
        ));
    }
    public function manualAction()
    {
        $p = $_POST;
        echo $p['id_oculto'] ;
        return new ViewModel(array(
            'titulo' => 'Manual de Ayuda al Usuario',
        ));
    }
    public function perfilAction()
    {
        return new ViewModel(array(
            'titulo' => 'Modificar Perfil',
        ));
    }
    }
